package cn.myfirefly.moming.house.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.controller.BaseCommonController;
import cn.myfirefly.moming.house.entity.SpfTradeReturnVisit;
import cn.myfirefly.moming.house.model.SpfTradeContractDetailDto;
import cn.myfirefly.moming.house.model.SpfTradeReturnVisitDto;
import cn.myfirefly.moming.house.model.SpfTradeReturnVisitFilterDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormFilterDto;
import cn.myfirefly.moming.house.model.SpfTradeWageDto;
import cn.myfirefly.moming.house.service.SpfTradeContractService;
import cn.myfirefly.moming.house.service.SpfTradeReturnVisitService;
import cn.myfirefly.moming.house.service.SpfTradeTrackFormService;
import cn.myfirefly.moming.house.service.SpfTradeWageService;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("spfTradeReturnVisit")
public class SpfTradeReturnVisitController extends BaseCommonController {

	@Autowired
	private SpfTradeReturnVisitService spfTradeReturnVisitService;
	
	@Autowired
	private SpfTradeTrackFormService spfTradeTrackFormService;
	
	@Autowired
	private SpfTradeContractService spfTradeContractService;
	
	@Autowired
	private SpfTradeWageService spfTradeWageService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "spftradereturnvisit/index";
	}
	
	@RequestMapping("mlist.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<SpfTradeTrackFormDto> mlist(SpfTradeTrackFormFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		this.wrapDataAuth(dto);
		PageResultDto<SpfTradeTrackFormDto> result = spfTradeTrackFormService.getList(dto);
		return result;
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradereturnvisit/edit";
	}
	
	@RequestMapping("show.do")
	public String show(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("funcId", "spfTradeReturnVisit");
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontract/show";
	}
	
	@RequestMapping("wagelist.do")
	@ResponseBody
	public PageResultDto<SpfTradeWageDto> wagelist(SpfTradeWageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return new PageResultDto<SpfTradeWageDto>();
		}
		dto.setTradeContractId(new Integer(contractId));
		PageResultDto<SpfTradeWageDto> result = spfTradeWageService.getList(dto);
		return result;
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradereturnvisit/add";
	}
	
	@RequestMapping("addSave.do")
	@ResponseBody
	public ResultDto addSave(SpfTradeReturnVisitDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() != null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request, true);
		spfTradeReturnVisitService.insert(dto);
		return success("保存成功");
	}
	
	@RequestMapping("editSave.do")
	@ResponseBody
	public ResultDto editSave(SpfTradeReturnVisitDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() == null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request);
		spfTradeReturnVisitService.update(dto);
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<SpfTradeReturnVisit> list(SpfTradeReturnVisitFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		PageResultDto<SpfTradeReturnVisit> result = spfTradeReturnVisitService.getList(
				new Integer(request.getParameter("contractId")), dto);
		return result;
	}
	
	@RequestMapping("get.do")
	@ResponseBody
	public ResultDto getSpfTradeReturnVisit(SpfTradeReturnVisitDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeReturnVisitDto spfTradeReturnVisit = spfTradeReturnVisitService.getSpfTradeReturnVisit(dto.getId());
		if (!StrUtil.equals(spfTradeReturnVisit.getCreateBy(), this.getLoginUser(request).getUserName())) {
			return error("当前数据不属于您，不能编辑");
		}
		return success(spfTradeReturnVisit);
	}
	
	@RequestMapping("getTrackForm.do")
	@ResponseBody
	public ResultDto getSpfTradeTrackForm(SpfTradeTrackFormDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeTrackFormDto spfTradeTrackForm = spfTradeTrackFormService.getSpfTradeTrackForm(dto.getId());
		return success(spfTradeTrackForm);
	}
	
	@RequestMapping("getDetail.do")
	@ResponseBody
	public ResultDto getContractDetail(HttpServletRequest request, 
			HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StrUtil.isBlank(id)) {
			return error("请求参数错误");
		}
		SpfTradeContractDetailDto dto = spfTradeContractService.getContractDetail(new Integer(id));
		return success(dto);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(SpfTradeReturnVisitDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = spfTradeReturnVisitService.del(dto.getId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
}
